<template>
	<view class="log-in">
		<view class="flex-col align-c mb50">
			<image class="logo-box mb12" src="../../../static/my/logo.png" mode=""></image>
			<view class="fz18 bold">
				欢迎登录汇福源
			</view>
		</view>
		<view class="account-box mb40">
			<view class="account-line flex align-c">
				<u-input v-model="phone" type="text" placeholder='请输入手机号' :clearable='false' />
			</view>
			<view class="account-line  flex align-c mb20">
				<u-input v-model="password" type="password" placeholder='请输入密码' :clearable='false' />
			</view>
			<view class="account-dss">
				<view class="">
					<u-checkbox v-model="checked"></u-checkbox>
					<view class="text">
						我已阅读并同意<text @click="goXiey(2)">《隐私政策协议》</text>
					</view>
				</view>
				<view class="fz12 c9" @click="jumpPath('/pages/subpackage/landing_operation/retrievePassword')">
					忘记密码?
				</view>
			</view>
		</view>
		<view class="log-in-btn-box flex-col align-c">
			<u-button class="mb15 log-in-btn" @click="logIn">登录</u-button>
			<view class="fz12 c9" v-if="applyStatus==0">
				还没有账号？<text style="color: #BC8523;"
					@click="jumpPath('/pages/subpackage/landing_operation/register')">点击注册</text>
			</view>
		</view>
	</view>
</template>

<script>
import {
	setTokenStorage
} from '@/utils/token';
import logger from '@/utils/logger';
import {
	genTestUserSig
} from '@/debug/GenerateTestUserSig.js';
const {
	getTokenStorage
} = require('@/utils/token.js');
const app = getApp();
import {
	userLogin,
	getUserinfo
} from '@/network/loginAndMy.js'
import {
		config,
	} from '@/network/api.js'
export default {
	data() {
		return {
			phone: '',
			password: '',
			userID: '',
			hidden: false,
			btnValue: '获取验证码',
			btnDisabled: false,
			privateAgree: false,
			code: '',
			sessionID: '',
			info: {},
			checked: false,
			applyStatus:''
		}
	},
	onLoad(option) {
		this.configRe()
		const that = this;
		this.setData({
			path: option.path
		});
		uni.getStorage({
			// 获取本地缓存
			key: 'sessionID',
			success(res) {
				that.setData({
					sessionID: res.data
				});
			}
		});
		uni.setStorage({
			key: 'path',
			data: option.path
		});
	},
	mounted() {

	},
	methods: {
		// 获取配置信息
		configRe() {
			config().then(res => {
				this.applyStatus = res.data.applyStatus
			})
		},
		// 是否同意
		gengx() {
			uni.$TUIKit.updateMyProfile({
				nick: this.info.nickname,
				avatar: this.info.avatar,
				gender: uni.$TUIKitTIM.TYPES.GENDER_MALE,
				selfSignature: '我的个性签名',
				allowType: uni.$TUIKitTIM.TYPES.ALLOW_TYPE_ALLOW_ANY
			}).then(imResponse => {
				console.log(imResponse, '成功11111111111111111111111111111')
			})
				.catch(err => {
					console.log(err, '报错00000000000000000000000')
				})
		},
		login() {
			const userID = this.userID;
			const userSig = genTestUserSig(userID).userSig;
			const SDKAppID = app.globalData.SDKAppID;
			logger.log(`TUI-login | login  | userSig:${userSig} userID:${userID}`);
			// #ifdef  APP-PLUS
			uni.$aegis.reportEvent({
				name: 'platform',
				ext1: 'platform-APP',
				ext2: 'uniTuikitExternal',
				ext3: `${SDKAppID}`,
			})
			// #endif
			// #ifdef MP-WEIXIN  
			uni.$aegis.reportEvent({
				name: 'platform',
				ext1: 'platform-MP-WEIXIN',
				ext2: 'uniTuikitExternal',
				ext3: `${SDKAppID}`,
			})
			// #endif
			// #ifdef H5
			uni.$aegis.reportEvent({
				name: 'platform',
				ext1: 'platform-H5',
				ext2: 'uniTuikitExternal',
				ext3: `${SDKAppID}`,
			})
			uni.showToast({
				title: 'TUIKit 暂不支持 H5 / web ，请使用者自己完成兼容哦～ ',
				icon: 'none',
				duration: 3000
			});
			// #endif
			app.globalData.userInfo = {
				userSig,
				userID
			};
			setTokenStorage({
				userInfo: app.globalData.userInfo
			});
			wx.setStorageSync(`TIM_${getApp().SDKAppID}_isTUIKit`, true);
			uni.$TUIKit.login({
				userID: userID,
				userSig: userSig
			}).then(() => {
				uni.$aegis.reportEvent({
					name: 'login',
					ext1: 'login-success',
					ext2: 'uniTuikitExternal',
					ext3: `${SDKAppID}`,
				})
			}).catch((error) => {
				uni.$aegis.reportEvent({
					name: 'login',
					ext1: `login-failed#error:${error}`,
					ext2: 'uniTuikitExternal',
					ext3: `${SDKAppID}`,
				})
			})

			// 登录原生插件
			// #ifdef APP-PLUS
			if (typeof (uni.$TUICalling) == 'undefined') {
				uni.showToast({
					title: '如果需要音视频功能，请集成插件使用真机运行并且自定义基座调试哦～',
					icon: 'none',
					duration: 3000
				});
				logger.error(
					'请使用真机运行并且自定义基座调试，否则影响音视频功能～ 插件地址：https://ext.dcloud.net.cn/plugin?id=7097 , 调试地址：https://nativesupport.dcloud.net.cn/NativePlugin/use/use'
				);
			} else {
				uni.$TUICalling.login({
					sdkAppID: SDKAppID,
					userID: userID,
					userSig: userSig
				},
					res => {
						console.log(JSON.stringify(res.msg));
						uni.showToast({
							title: 'login',
							icon: 'none'
						});
					}
				);
			}

			// #endif
			uni.showToast({
				title: "登陆成功",
				icon: "none",
				duration: 2000,
			});
			setTimeout(() => {
				this.gengx()
			}, 1000)

			// if (this.path && this.path !== 'undefined') {
			// 	uni.redirectTo({
			// 		url: this.path
			// 	});
			// } else {
			// 	uni.switchTab({
			// 		url: '../TUI-Index/index'
			// 	});
			// }
		},
		// 跳转到协议页面
		goXiey(type) {
			uni.navigateTo({
				url: '/pages/subpackage/my/policy?type=' + type
			})
		},
		// 登陆
		logIn() {
			if (!this.contentVerify()) {
				if (this.checked) {
					let params = {
						account: this.phone,
						password: this.password,
					}
					userLogin(params).then(res => {
						if (res.code == 1) {
							uni.removeStorageSync('indexp')
							uni.setStorageSync('token1', res.data.userinfo.token);
							uni.setStorageSync('phone', this.phone);
							this.getUserinfoRe()
							uni.switchTab({
								url: '/pages/main/index/index'
							});
						}
					})
				} else {
					uni.showToast({
						title: '请阅读后勾选隐私政策协议',
						duration: 2000,
						icon: 'none'
					});
				}

			}
		},
		// 获取用户信息
		getUserinfoRe() {
			getUserinfo().then(res => {
				if (res.code == 1) {
					this.info = res.data
					this.userID = res.data.ownid + ''
					this.login()
				}
			})
		},
		// 跳转页面
		jumpPath(path) {
			if (path == undefined) return
			uni.navigateTo({
				url: path
			});
		},
		// 输入内容验证
		contentVerify() {
			if (this.phone == '') {
				uni.showToast({
					title: '手机号不可为空',
					duration: 2000,
					icon: 'none'
				});
				return true
			}

			if (this.password == '') {
				uni.showToast({
					title: '请输入密码',
					duration: 2000,
					icon: 'none'
				});
				return true
			}

			return false
		},
	}
}
</script>

<style lang="scss">
.log-in {
	padding: 100rpx 0 0;
	height: 100vh;
	box-sizing: border-box;

	.logo-box {
		width: 130rpx;
		height: 130rpx;
	}

	.account-box {
		padding: 0 30rpx;
		box-sizing: border-box;
	}

	.account-line {
		height: 110rpx;
		border-bottom: 1rpx solid #EDEDED;
	}

	.account-dss {
		font-size: 24rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		>view:nth-child(1) {
			display: flex;

			.text {
				margin-left: -20rpx !important;

				>text {
					margin-left: 20rpx;
					color: #b48739;
				}
			}
		}


	}

	.log-in-btn-box {
		padding: 0 72rpx;

		.log-in-btn {
			width: 100%;
			background: linear-gradient(0deg, #b07412 0%, #e3ba5b 100%) !important;
			border-radius: 10rpx;
			height: 90rpx;
			line-height: 90rpx;
			text-align: center;
			padding: 0 !important;
			border: none !important;
			color: #ffffff;
		}

		uni-button:after {
			content: " ";
			border: none !important;
		}
	}
}
</style>